﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows;
using System.Windows.Controls;

namespace Com.CodePlex.SPPower.View {

    public class TreeViewCommand : DependencyObject {

        /*
     Public Shared ReadOnly SelectedItemChangedCommandProperty As DependencyProperty _
      = DependencyProperty.RegisterAttached(name:="SelectedItemChangedCommand", _
         propertyType:=GetType(ICommand), _
         ownerType:=GetType(TreeViewCommands), _
         defaultMetadata:=New PropertyMetadata(defaultValue:=Nothing))

     Private Shared _selectedItemChangedHandlers As New Dictionary(Of TreeView, SelectedItemChangedHandler)

     Private Sub New()
      'This class is a helper class and should not be instantiated directly
     End Sub

     Public Shared Function GetSelectedItemChangedCommand(ByVal target As TreeView) As ICommand
      Return target.GetValue(SelectedItemChangedCommandProperty)
     End Function

     Public Shared Sub SetSelectedItemChangedCommand(ByVal target As TreeView, ByVal command As ICommand)
      target.SetValue(SelectedItemChangedCommandProperty, command)

      If Not _selectedItemChangedHandlers.ContainsKey(target) Then
       _selectedItemChangedHandlers.Add(target, New SelectedItemChangedHandler(target, command))
      Else
       If _selectedItemChangedHandlers(target) IsNot Nothing Then _selectedItemChangedHandlers(target).Dispose()
       _selectedItemChangedHandlers(target) = New SelectedItemChangedHandler(target, command)
      End If
     End Sub

     Private Class SelectedItemChangedHandler
      Implements IDisposable

      Private _treeView As TreeView
      Private _command As ICommand

      Public Sub New(ByVal treeView As TreeView, ByVal command As ICommand)
       _treeView = treeView
       _command = command

       AddHandler _treeView.SelectedItemChanged, AddressOf TreeView_SelectedItemChanged
      End Sub

      Private Sub TreeView_SelectedItemChanged(ByVal sender As Object, ByVal e As RoutedPropertyChangedEventArgs(Of Object))
       _command.Execute(_treeView)
      End Sub

      Private disposedValue As Boolean = False  ' To detect redundant calls

      ' IDisposable
      Protected Overridable Sub Dispose(ByVal disposing As Boolean)
       If Not Me.disposedValue Then
        RemoveHandler _treeView.SelectedItemChanged, AddressOf TreeView_SelectedItemChanged
       End If
       Me.disposedValue = True
      End Sub

    #Region " IDisposable Support "
      ' This code added by Visual Basic to correctly implement the disposable pattern.
      Public Sub Dispose() Implements IDisposable.Dispose
       ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
       Dispose(True)
       GC.SuppressFinalize(Me)
      End Sub
    #End Region

     End Class

    End Class
    You can use this attached property like so:
        /*
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Sandbox1"
        Title="Window1">
        <Grid>
            <TreeView local:TreeViewCommands.SelectedItemChangedCommand="{Binding Command}" >
            </TreeView>
        </Grid>
    </Window>
         * */
    }
}
